package main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Retriever {

	private List<String> myList;
	private String myUrl;
	
	public Retriever(String the_url){
		myUrl = the_url;
		myList = new ArrayList<String>();
	}
	
	private String Trimmed(String the_string){
		 String trimmedString = "";
		int temp =  the_string.indexOf('#');
		 if (temp >= 0){
			 trimmedString =  the_string.substring(0,temp);
			 the_string =  trimmedString;
		 }
		 
		 
		 return the_string;
	 }
	/*
	private	boolean isValid2(String the_link){
		boolean flag = true;
		if(the_link.matches("http://questioneverything.typepad.com/")){
		flag = false;
		} else if(!the_link.startsWith("http:")){
			flag = false;
		} else if (!the_link.endsWith("html")||!the_link.endsWith("/")){
			flag = false;
		} else if(!the_link.endsWith("htm")||!the_link.endsWith("txt")){
			flag = false;
		}
		return flag;
	}
	*/
	
	
	private boolean isLink(String the_link){
		boolean flag = true;
		try {
			 Jsoup.connect(the_link).timeout(0).get();
			} catch (IOException e) {
				// TODO Auto-generated catch block
			System.out.println("Error on: " + the_link);
				flag = false;
			}
		return flag;
	}
	/**
	 * Validates the URLs we want to visit.
	 * */
	private boolean isValid(String the_link){
		//System.out.println(the_link);
		boolean flag = true;
	
		if (the_link.endsWith(".html")||the_link.endsWith("/")||the_link.endsWith(".txt")||the_link.endsWith(".htm")){
			  
			flag = true;
			
		} 
		if(!the_link.startsWith("http:")){
			flag = false;
		} 
		if(the_link.startsWith("http://www.questioneverything.typepad.com")){
		flag = false;
		
		}
		if(the_link.startsWith("http://www.youtube.com")){
		flag = false;

		}
		if(the_link.startsWith("http://www.facebook.com")){
		flag = false;
	
			
		} 
		if (the_link.endsWith(".m4v")){//||the_link.endsWith("/")){
			flag = false;
			}
	

		return flag;
	}
	/*
	private	boolean isValidinside(String the_OuterLink, String the_link){
		StringBuffer theLink = new StringBuffer();
	 	
	 		theLink.append(myUrl);
	 		theLink.append(the_OuterLink);
			String  myString = theLink.toString();
			
			boolean flag = false;
			if (myString.matches(the_link)){
				myString = Trimmed(myString);
				if (myString.endsWith("html")|| myString.endsWith("/")){
					
					flag = true;
				}
			
			} 
			
			return flag;
		}
		*/
	public List<String> getLinks() {
	   // String url = "http://faculty.washington.edu/gmobus/";
	    Document doc = null;
		try {
			doc = Jsoup.connect(myUrl).timeout(0).get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	    Elements links = doc.select("a[href]");
	   
	    for (Element link : links) {
	    	        	
	    	if (isValid(link.attr("abs:href"))) {
	    	
	    		myList.add(link.attr("abs:href"));
	  
	    	//	}
	    	//System.out.println(link.attr("abs:href"));
	    	}
	    	
	 
	    }
		return myList;
	}
	

}
